﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using WinnerCMS.Arguments;
using WinnerCMS.Exceptions;
using WinnerCMS.Common;

namespace WinnerCMS.PlugIn.Indexer
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            CommandLineParser parser = CreateParser();

            try
            {
                parser.ParseCommandLine(args);
            }
            catch (CommandLineException e)
            {
                Console.WriteLine(e.Message);
                parser.ShowUsage();
                Console.ReadKey();
                return;
            }
           

            Console.WriteLine("正在索引...");
            DateTime start = DateTime.Now;

            string docIndexDirectory = ((ValueArgument<string>)parser.LookupArgument("IndexDirectory")).Value;

            if (docIndexDirectory.IsNullOrEmpty())
            {
                docIndexDirectory = Application.StartupPath;
                docIndexDirectory = docIndexDirectory.Substring(0, docIndexDirectory.Length - 4);
                docIndexDirectory += @"\FullTextIndex";
            }

            IndexData indexer = new IndexData(docIndexDirectory)
            {
                LotSize = ((ValueArgument<int>)parser.LookupArgument("LotSize")).Value,
                Action = ((ValueArgument<IndexData.IndexAction>)parser.LookupArgument("Action")).Value,
            };

            indexer.IndexContent();

            Console.WriteLine("正在优化...");

            indexer.Dispose();

            Console.WriteLine("耗时: " + (DateTime.Now - start).TotalSeconds + "秒");
            Console.ReadKey();

        }

        private static CommandLineParser CreateParser()
        {
            CommandLineParser parser = new CommandLineParser();
            var indexDirectoryArgument = new ValueArgument<string>('d', "IndexDirectory", "索引目录")
            {
                Optional = false,
                ConvertValueHandler = (stringValue) => stringValue
            };

            var lotSizeArgument = new ValueArgument<int>('l', "LotSize", "多少一批")
            {
                Optional = true,
                ConvertValueHandler = (value) => Convert.ToInt32(value) > 0 ? Convert.ToInt32(value) : 1
            };

            var actionArgument = new ValueArgument<IndexData.IndexAction>('a', "Action", "生成索引动作")
            {
                Optional = true,
                ConvertValueHandler = (value) =>
                {
                    int i = Convert.ToInt32(value) > 0 ? Convert.ToInt32(value) : 1;
                    return (IndexData.IndexAction) i;
                }
            };
            FileArgument helpArgument = new FileArgument('?', "help", "帮助")
            {
                FileMustExist = false,
                Optional = true
            };

            parser.Arguments.Add(indexDirectoryArgument);
            parser.Arguments.Add(lotSizeArgument);
            parser.Arguments.Add(actionArgument);
            parser.Arguments.Add(helpArgument);

            parser.IgnoreCase = true;
            return parser;
        }
 
    }
}